package com.zretc.demojparedis.dao;


import com.zretc.demojparedis.entity.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.LockModeType;
import java.io.Serializable;
import java.util.List;



public interface QuestionDao extends JpaRepository<Question,Integer>, JpaSpecificationExecutor<Question>, Serializable {
    //自定义方法来,满足JPA语法规范的，模糊

    List<Question> findByQtitleContaining(String qtitle);


    //使用不满足规范的@Query来查询,自定义
    //?1第一个参数
    @Query("from Question where qdate>?1")
    List<Question> getQuestionByDate(String date);


    //测试悲观锁

    /**
     * 查询单个question对象
     * @param qid
     * @return
     */
     @Lock(LockModeType.PESSIMISTIC_WRITE)
     @Query("from Question where qid=?1 ")
     Question getQuestionByIdlock(int qid);



    /**
     *
     *  分页的返回结果是page对象，注意包注意包:
     *
     *    一个参数关键字,
     *   另外一个参数 Pageable ,封装页码pageNum/page，每页记录数pageSize,limit
     *
     * @param keywords 查询关键字
     * @param pageable  pageable对象
     * @return  Page
     *
     */
    @Query("select q from Question q where q.qtitle like %?1%")
    Page<Question> getQuestionByPage(String keywords, Pageable pageable);

}
